<?php

namespace Model\ExamBundle\Controller;

use Model\CourseCategoryBundle\Model\CourseCategoryModel;
use Model\ExamBundle\Entity\Exam;
use Model\ExamBundle\Entity\ExamQuestion;
use Model\ExamBundle\Entity\ExamQuestionGroup;
use Model\ExamBundle\Entity\ExamStatic;
use Model\ExamBundle\Model\ExamModel;
use Model\ExamBundle\Setting\ExamSetting;
use Model\QuestionBundle\Entity\Choice;
use Model\QuestionBundle\Model\QuestionModel;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Method;

use Symfony\Component\HttpFoundation\Request;
use Base\BaseController;
use Model\ExamBundle\Entity\Exam as Entity;

/**
 * @route("/admin/")
 */
class ExamAdminController extends BaseController
{

    /**
     * @route("Exam/add",name="ExamPreAdd")
     */
    public function add(Request $request)
    {
        if ($request->getMethod() == "POST") {
            var_dump($_REQUEST);
            if (!$type = $this->getParamRequired("type")) {
                if ($type !== "0") {
                    return $this->msg_out("请选择题型");
                }
            }
            if (!$title = $this->getParamRequired("title")) {
                return $this->msg_out("请填写题干");
            }
            if (!$Point = $this->getParamRequired("Point")) {
                return $this->msg_out("请设置知识点");
            }
            if (!$totalScore = $this->getParamRequired("totalScore")) {
                return $this->msg_out("请设置总分");
            }
            if (!$totalTime = $this->getParamRequired("totalTime")) {
                return $this->msg_out("请设置考试时间");
            }
            if (!$intro = $this->getParamRequired("intro")) {
                return $this->msg_out("请设置考试要求");
            }
            $em = $this->getDoctrine()->getManager();
            $exam = new Exam();
            $exam->setType($type);
            $exam->setSort($Point);
            $exam->setTitle($title);
            $exam->setIntro($intro);
            $exam->setTotalScore($totalScore);
            $exam->setTotalTime($totalTime);
            $exam->setUserId(0);

            $em->persist($exam);
            $em->flush();

            $examStatic = new ExamStatic();
            $examStatic->setExamId($exam->getId());

            $em->persist($examStatic);
            $em->flush();

            return $this->lists();
        } else {
            $CourseCategory = new CourseCategoryModel($this->getDoctrine());
            $groups = $CourseCategory->getAllGroupByParent();

            $data = array(
                "sorts" => json_encode($groups),
            );
            return $this->render("ModelExamBundle:Exam:add_advance.php.twig",$data);
        }
    }

    /**
     * @route("Exam/Sort/{id}",name="ExamSort")
     *
     * @Method({"GET", "POST"})
     */
    public function ExamSort(Request $request , Exam $Exam)
    {
        if ($request->getMethod() == "POST") {
            if (!$Point = $this->getParamRequired("Point")) {
                return $this->msg_out("请设置知识点");
            }

            $em = $this->getDoctrine()->getManager();
            $Exam->setSort($Point);
            $em->persist($Exam);
            $em->flush();

            return $this->lists();
        } else {
            $CourseCategory = new CourseCategoryModel($this->getDoctrine());
            $groups = $CourseCategory->getAllGroupByParent();
            $rep = $this->getDoctrine()->getRepository("ModelCourseCategoryBundle:CourseCategory");
            $Sort = $rep->find($Exam->getSort());
            $link = $Sort->getLink();
            $link = str_replace("]","",$link);
            $linkArr = explode("[",substr($link,1));

            $data = array(
                "sorts" => json_encode($groups),
                "Exam" => $Exam,
                "Sort" => json_encode($linkArr),
            );
            return $this->render("ModelExamBundle:Exam:edit_point.php.twig",$data);
        }
    }

    /**
     * @route("Exam/Question/{id}",name="ExamQuestion")
     *
     * @Method({"GET", "POST"})
     */
    public function ExamQuestion(Exam $Exam)
    {
        $data = array(
            "Exam" => $Exam,
        );
        return $this->render("ModelExamBundle:Exam:edit_question.php.twig",$data);
    }

    /**
     * @route("Exam/ExamAddQuestionGroup/{id}",name="ExamAddQuestionGroup")
     *
     * @Method({"GET"})
     */
    public function ExamAddQuestionGroup(Exam $Exam)
    {
        if (!$GroupTitle = $this->getParamRequired("GroupTitle")) {
            return $this->json_out(0,"GroupTitle missed");
        }
        if (!$GroupIntro = $this->getParamRequired("GroupIntro")) {
            $GroupIntro = "";
        }
        $examQuestionGroup = new ExamQuestionGroup();
        $examQuestionGroup->setTitle($GroupTitle);
        $examQuestionGroup->setIntro($GroupIntro);
        $examQuestionGroup->setOrders(time());
        $examQuestionGroup->setExamId($Exam->getId());
        $em = $this->getDoctrine()->getManager();
        $em->persist($examQuestionGroup);
        $em->flush();
        $examId = $Exam->getId();
        $examModel = new ExamModel($this->getDoctrine());
        //获取所有题目
        $questions = $examModel->getQuestion4Exam($examId);
        return $this->json_out(1,"获取成功",$questions);
    }


    /**
     * @route("Exam/GetQuestion/{id}",name="ExamGetQuestion")
     *
     * @Method({"GET"})
     */
    public function ExamGetQuestion(Exam $Exam)
    {
        $examId = $Exam->getId();
        $examModel = new ExamModel($this->getDoctrine());
        //获取所有题目
        $questions = $examModel->getQuestion4Exam($examId);
        return $this->json_out(1,"获取成功",$questions);
    }
    /**
     * @route("Exam/GroupEdit/{id}",name="ExamGroupEdit")
     *
     * @Method({"POST"})
     */
    public function ExamGroupEdit(Exam $Exam)
    {
        if (!$id = $this->getParamRequired("id")) {
            return $this->json_out(0,"数据异常");
        }
        if (!$action = $this->getParamRequired("action")) {
            return $this->json_out(0,"行为异常");
        }
        $checkExamId = $Exam->getId();
        $ExamModel = new ExamModel($this->getDoctrine());
        $rst = $ExamModel->modGroupOrders($id,$action,$checkExamId);


        $examModel = new ExamModel($this->getDoctrine());
        //获取所有题目
        $questions = $examModel->getQuestion4Exam($checkExamId);
        return $this->json_out(1,"修改成功",$questions);
    }
    /**
     * @route("Exam/QuestionEdit/{id}",name="ExamQuestionEdit")
     *
     * @Method({"POST"})
     */
    public function ExamQuestionEdit(Exam $Exam)
    {
        if (!$id = $this->getParamRequired("id")) {
            return $this->json_out(0,"数据异常");
        }
        if (!$action = $this->getParamRequired("action")) {
            return $this->json_out(0,"行为异常");
        }
        $checkExamId = $Exam->getId();
        $ExamModel = new ExamModel($this->getDoctrine());
        $this->log_out($_REQUEST);
        $rst = $ExamModel->modExamQuestionOrders($id,$action,$checkExamId);
        $this->log_out($rst);

        $examModel = new ExamModel($this->getDoctrine());
        //获取所有题目
        $questions = $examModel->getQuestion4Exam($checkExamId);
        return $this->json_out(1,"修改成功",$questions);
    }




    /**
     * @route("Exam/GetQuestion4Choose/{id}",name="ExamGetQuestion4Choose")
     *
     * @Method({"GET"})
     */
    public function ExamGetQuestion4Choose(Exam $Exam)
    {
        if (!$type = $this->getParamRequired("type")) {
            $type = 0;
        }
        //获取所有题目
        $QuestionModel = new QuestionModel($this->getDoctrine());
        $questions = $QuestionModel->getBySort4Exam($Exam->getSort(),$Exam->getId(),$type,0,1000000);
        $data = array();
        foreach ($questions as &$question) {
            $questionId = $question["id"];
            $question["Choice"] = $QuestionModel->getChoices($questionId);
            if ($question["examId"] == null) {
                $data[] = $question;
            }
        }

        return $this->json_out(1,"获取成功",$data);
    }
    /**
     * @route("Exam/ExamAddQuestion/{id}",name="ExamAddQuestion")
     *
     * @Method({"POST"})
     */
    public function ExamAddQuestion(Exam $Exam)
    {
        if (!$questionId = $this->getParamRequired("questionId")) {
            return $this->msg_out("未选中任何题目");
        }
        if (!$groupId = $this->getParamRequired("groupId")) {
            $groupId = 0;
        }
        if (!$Score = $this->getParamRequired("Score")) {
            $Score = 1;
        }

        $rep_group = $this->getDoctrine()->getRepository("ModelExamBundle:ExamQuestionGroup");
        $rep_exam_questions = $this->getDoctrine()->getRepository("ModelExamBundle:ExamQuestion");

        $examGroup =$rep_group->find($groupId);

        if (!$ExamQuestion = $rep_exam_questions->findOneBy(array("examId"=>$Exam->getId(),"questionId"=>$questionId))) {
            $ExamQuestion = new ExamQuestion();
        }

        if ($examGroup && $examGroup->getExamId() == $Exam->getId()) {

            $ExamQuestion->setGroups($groupId);
        } else {
            $ExamQuestion->setGroups(0);
        }

        $ExamQuestion->setExamId($Exam->getId());
        $ExamQuestion->setOrders(time());
        $ExamQuestion->setQuestionId($questionId);
        $ExamQuestion->setScore($Score);
        $em = $this->getDoctrine()->getManager();
        $em->persist($ExamQuestion);
        $em->flush();
        $examId = $Exam->getId();
        $examModel = new ExamModel($this->getDoctrine());
        //获取所有题目
        $questions = $examModel->getQuestion4Exam($examId);
        return $this->json_out(1,"获取成功",$questions);
    }












    /**
     * @route("Exam/add/do",name="ExamDoAdd")
     *
     * @Method({"GET", "POST"})
     */
    public function doAdd(Request $request)
    {
        //私有代码
        $entity = new Entity();

        //通用代码
        $this->setSetting(new ExamSetting($this->getDoctrine()->getManager()));
        return $this->comDoAdd($request,$entity);
    }

    /**
     * @route("Exam/edit/{id}",name="ExamPreEdit")
     */
    public function edit(Entity $entity)
    {
        $this->setSetting(new ExamSetting($this->getDoctrine()->getManager()));
        return $this->comEdit($entity);
    }

    /**
     * @route("Exam/edit/{id}/do",name="ExamDoEdit")
     *
     * @Method({"GET", "POST"})
     */
    public function doEdit(Request $request,Entity $entity)
    {
        $this->setSetting(new ExamSetting($this->getDoctrine()->getManager()));
        return $this->comDoEdit($request,$entity);
    }

    /**
     * @route("Exam",name="ExamLists")
     */
    public function lists()
    {
        //私有部分
        $tools = array();

        //通用部分
        $this->setSetting(new ExamSetting($this->getDoctrine()->getManager()));
        return $this->comLists($tools);
    }


    /**
     * @route("Exam/tools",name="ExamTools")
     */
    public function tools(Request $request)
    {
        $this->setSetting(new ExamSetting($this->getDoctrine()->getManager()));
        return $this->comTools($request);
    }

}
